//
//(C) Copyright 2011 HongKong Airline.,Ltd. 
//
/**
 * systemUserServiceImpl
 * 项目名称:代购系统
 *
 * @author Mengxiangliang
 *
 * 功能描述:
 * 
 * 历史版本:2013-01-04 v1.0.0 Mengxiangliang  创建:
 * 
 */
 
package com.gamutsoft.amb.user.service.impl;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import org.springframework.security.authentication.encoding.Md5PasswordEncoder;

import com.gamutsoft.amb.account.model.Account;
import com.gamutsoft.amb.account.service.IAccountService;
import com.gamutsoft.amb.common.bean.QueryExample;
import com.gamutsoft.amb.common.constants.Constants;
import com.gamutsoft.amb.common.exception.BusinessException;
import com.gamutsoft.amb.common.service.impl.GenericServiceImpl;
import com.gamutsoft.amb.common.util.VDateTime;
import com.gamutsoft.amb.user.dao.ISystemUserDao;
import com.gamutsoft.amb.user.model.SystemUser;
import com.gamutsoft.amb.user.service.ISystemUserService;

public class SystemUserServiceImpl extends GenericServiceImpl<SystemUser, Long> implements ISystemUserService {
	public ISystemUserDao systemUserDao;
	
	private IAccountService accountService;

    public IAccountService getAccountService() {
		return accountService;
	}

	public void setAccountService(IAccountService accountService) {
		this.accountService = accountService;
	}

	public SystemUserServiceImpl(ISystemUserDao systemUserDao) {
		super(systemUserDao);
		this.systemUserDao = systemUserDao;
	}
	
	/**
	 * 根据条件查询记录
	 * @author Mengxiangliang
	 * @param  QueryExample
	 * @return SystemUserList
	 * @throws BusinessException
	 */
	public List<Map> searchSystemUserList(QueryExample qe) throws BusinessException{
	  
	   return systemUserDao.searchSystemUserList(qe);
	   
	}
	
	/**
	 * 根据ID查询记录
	 * @author Mengxiangliang
	 * @param Long id
	 * @return domain
	 * @throws BusinessException
	 */
	public SystemUser searchSystemUserById(Long id) throws BusinessException{
	  
	   return systemUserDao.searchSystemUserById(id);
	   
	}

	/**
	 *  删除记录
	 * @author Mengxiangliang	 
	 * @param id
	 * @throws BusinessException
	 */
	public void deleteSystemUserById(Long id) throws BusinessException{
	 
	  systemUserDao.deleteSystemUserById(id);
	  
	}
	
	/**
	 * 修改记录
	 * @author Mengxiangliang
	 * @param domain
	 * @throws BusinessException
	 */
	public void updateSystemUser(SystemUser domain) throws BusinessException{
	
	   systemUserDao.save(domain);
	   
	}

	/**
	 * 新增记录
	 * @author Mengxiangliang
	 * @param domain
	 * @return
	 * @throws BusinessException
	 */
	public SystemUser createSystemUser(SystemUser domain) throws BusinessException{
		SystemUser systemUser=null;
	   //创建用户
		if(domain!=null){
			//密码加密
			Md5PasswordEncoder  mp = new Md5PasswordEncoder();
			String password=mp.encodePassword(domain.getPassword(), Constants.DAIGOU_KEY);
			domain.setPassword(password);
			domain.setStatus(Constants.STATE_VALID);//初始为有效
			domain.setCreateTime(VDateTime.getCurrentStandardTime());
			systemUser=systemUserDao.save(domain);
			//生成账户
			Account account=new Account();
			account.setBalance(new BigDecimal("0.00"));
			account.setCreateTime(VDateTime.getCurrentStandardTime());
			account.setIntegral(0L);
			account.setPassword(password);//账户初始密码与登录密码相同
			account.setStatus(Constants.STATE_VALID);//初始为有效
			account.setUserId(systemUser.getId());
			accountService.createAccount(account);
		}
		return systemUser;
	}

	@Override
	public List<Map> searchSystemUserListByWhere(QueryExample qe,
			SystemUser domain) throws BusinessException {
		// TODO Auto-generated method stub
		return systemUserDao.searchSystemUserListByWhere(qe, domain);
	}

	@Override
	public SystemUser loginByUser(SystemUser domain) throws BusinessException {
		// TODO Auto-generated method stub
		return systemUserDao.loginByUser(domain);
	}
	

}